/*
* PickupDropoff Module Add New Location Model JS
*
* JSLint.com Check:
*/

// all the form items: name, address1-3, state, postcode, country, notes, gmaps checkbox, enabled checkbox
RM.Pages.PickupDropoff_Locations_ID = {
    xtype : "hidden",
    id : "rm_module_pickupdropoff_config_id",
    name : "rm_module_pickupdropoff_config_id"
};

// form panels

RM.Pages.PickupDropoff_Locations_Address_Fieldset = {
        xtype : "fieldset",
        title: RM.Translate.Admin.PickupDropoff.Locations.Edit.LocationInfo,
        autoHeight: true,
        layout: 'form',
        bodyBorder : false,
        frame : false,
        items : [
            {
                xtype : "textfield",
                id : "rm_module_pickupdropoff_config_name",
                name : "rm_module_pickupdropoff_config_name",
                fieldLabel : RM.Translate.Admin.PickupDropoff.Locations.Edit.Name,
                width : 200
            },{
                xtype : "textfield",
                id : "rm_module_pickupdropoff_config_address1",
                name : "rm_module_pickupdropoff_config_address1",
                fieldLabel : RM.Translate.Admin.PickupDropoff.Locations.Edit.Address1,
                width : 200
            },{
                xtype : "textfield",
                id : "rm_module_pickupdropoff_config_address2",
                name : "rm_module_pickupdropoff_config_address2",
                fieldLabel : RM.Translate.Admin.PickupDropoff.Locations.Edit.Address2,
                width : 200
            },{
                xtype : "textfield",
                id : "rm_module_pickupdropoff_config_address3",
                name : "rm_module_pickupdropoff_config_address3",
                fieldLabel : RM.Translate.Admin.PickupDropoff.Locations.Edit.Address3,
                width : 200
            },{
                xtype : "textfield",
                id : "rm_module_pickupdropoff_config_city",
                name : "rm_module_pickupdropoff_config_city",
                fieldLabel : RM.Translate.Admin.PickupDropoff.Locations.Edit.City,
                width : 200
            },{
                xtype : "textfield",
                id : "rm_module_pickupdropoff_config_state",
                name : "rm_module_pickupdropoff_config_state",
                fieldLabel : RM.Translate.Admin.PickupDropoff.Locations.Edit.State,
                width : 200
            },{
                xtype : "textfield",
                id : "rm_module_pickupdropoff_config_postcode",
                name : "rm_module_pickupdropoff_config_postcode",
                fieldLabel : RM.Translate.Admin.PickupDropoff.Locations.Edit.Postcode,
                width : 200
            },{
                xtype : "textfield",
                id : "rm_module_pickupdropoff_config_country",
                name : "rm_module_pickupdropoff_config_country",
                fieldLabel : RM.Translate.Admin.PickupDropoff.Locations.Edit.Country,
                width : 200
            }
        ]
};
RM.Pages.PickupDropoff_Locations_Options_Fieldset = {
        xtype : "fieldset",
        title: RM.Translate.Admin.PickupDropoff.Locations.Edit.Options,
        height: 130,
        layout: 'form',
        bodyBorder : false,
        frame : false,
        items : [
            {
                id : "rm_module_pickupdropoff_config_gmaps",
                xtype : "xcheckbox",
                name : "rm_module_pickupdropoff_config_gmaps",
                inputValue : "1",
                fieldLabel : RM.Translate.Admin.PickupDropoff.Locations.Edit.GMaps
            },{
                id : "rm_module_pickupdropoff_config_enabled",
                xtype : "xcheckbox",
                name : "rm_module_pickupdropoff_config_enabled",
                inputValue : "1",
                fieldLabel : RM.Translate.Admin.PickupDropoff.Locations.Edit.Enabled
            }
        ]
};
RM.Pages.PickupDropoff_Locations_Units_Fieldset = {
        xtype : "fieldset",
        title: RM.Translate.Admin.PickupDropoff.Locations.Edit.AssignUnits,
        autoHeight: true,
        layout: 'form',
        autoWidth: true,
        bodyBorder : false,
        frame : false,
        items : [
            RM.Pages.Functions.Units_Selector
        ]
};

// the main panel/window
RM.Pages.PickupDropoff_Locations_Add_MainPanel = new Ext.Window({
    id : 'rm_pages_pickupdropoff_locations_add_mainpage',
    title : RM.Translate.Admin.PickupDropoff.Locations.Edit.AddNewLocation,
    layout:'column',
    width: 700,
    height: 400,
    autoDestroy: false,
    closable: false,
    model: true,
    items : [{
        xtype: "panel",
        width: 400,
        bodyStyle : "padding:10px;",
        bodyBorder : false,
        frame : false,
        items:[
            RM.Pages.PickupDropoff_Locations_ID,
            RM.Pages.PickupDropoff_Locations_Address_Fieldset
        ]
    },{
        xtype: "panel",
        width: 283,
        bodyStyle : "padding:10px;",
        bodyBorder : false,
        frame : false,
        items:[
            RM.Pages.PickupDropoff_Locations_Units_Fieldset,
            RM.Pages.PickupDropoff_Locations_Options_Fieldset
        ]
    }],
    listeners: {
        'afterlayout': function(){

            // clear the feilds
            Ext.getCmp('rm_module_pickupdropoff_config_id').setValue("");
            Ext.getCmp('rm_module_pickupdropoff_config_name').setValue("");
            Ext.getCmp('rm_module_pickupdropoff_config_address1').setValue("");
            Ext.getCmp('rm_module_pickupdropoff_config_address2').setValue("");
            Ext.getCmp('rm_module_pickupdropoff_config_address3').setValue("");
            Ext.getCmp('rm_module_pickupdropoff_config_city').setValue("");
            Ext.getCmp('rm_module_pickupdropoff_config_state').setValue("");
            Ext.getCmp('rm_module_pickupdropoff_config_postcode').setValue("");
            Ext.getCmp('rm_module_pickupdropoff_config_country').setValue("");
            Ext.getCmp('rm_module_pickupdropoff_config_gmaps').setValue(0);
            Ext.getCmp('rm_module_pickupdropoff_config_enabled').setValue(0);

            // get the list selection
            var selection = RM.Pages.PickupDropoff_Locations_List_Columns_SM.getSelections();
            if (selection.length>0){
                selection = selection[0].id;
            } else {
                selection = null
            }

            // clear the list selection
            RM.Pages.PickupDropoff_Locations_List_Columns_SM.clearSelections();

            var myMask = new Ext.LoadMask('rm_pages_pickupdropoff_locations_add_mainpage', {msg:RM.Translate.Common.PleaseWait});
            myMask.show();

            var conn = new Ext.data.Connection();
            var request = {
                url: RM.Common.AssembleURL({
                    controller : 'PickupDropoff',
                    action: 'loadlocationjson',
                    parameters : [
                        {
                            name : 'id',
                            value : selection
                        }
                    ]
                }),
                method: 'POST',
                success: function(responseObject) {
                    var jsonObject = Ext.util.JSON.decode(responseObject.responseText);

                    if (jsonObject.settings.length>0){
                        Ext.getCmp('rm_module_pickupdropoff_config_id').setValue(jsonObject.settings[0].id);
                        Ext.getCmp('rm_module_pickupdropoff_config_name').setValue(jsonObject.settings[0].name);
                        Ext.getCmp('rm_module_pickupdropoff_config_address1').setValue(jsonObject.settings[0].address1);
                        Ext.getCmp('rm_module_pickupdropoff_config_address2').setValue(jsonObject.settings[0].address2);
                        Ext.getCmp('rm_module_pickupdropoff_config_address3').setValue(jsonObject.settings[0].address3);
                        Ext.getCmp('rm_module_pickupdropoff_config_city').setValue(jsonObject.settings[0].city);
                        Ext.getCmp('rm_module_pickupdropoff_config_state').setValue(jsonObject.settings[0].state);
                        Ext.getCmp('rm_module_pickupdropoff_config_postcode').setValue(jsonObject.settings[0].postcode);
                        Ext.getCmp('rm_module_pickupdropoff_config_country').setValue(jsonObject.settings[0].country);
                        Ext.getCmp('rm_module_pickupdropoff_config_gmaps').setValue(jsonObject.settings[0].gmaps);
                        Ext.getCmp('rm_module_pickupdropoff_config_enabled').setValue(jsonObject.settings[0].enabled);

                        RM.Pages.Functions.Units_Selector_SetSelection(jsonObject.settings[0].units, jsonObject.totalUnits);
                    }

                    myMask.hide();

                },
                failure: function() {
                    Ext.MessageBox.alert(RM.Translate.Common.Failed);
                    myMask.hide();
                }
            };
            conn.request(request);
        }
    },
    buttons: [
        {
            text: RM.Translate.Common.Save,
            handler: function(){
                RM.Pages.Functions.PickupDropoff_Locations_Save();
            }
        },{
            text: RM.Translate.Common.Cancel,
            handler: function(){
                RM.Pages.PickupDropoff_Locations_Add_MainPanel.hide();
            }
        }
    ]
});

// functions

RM.Pages.Functions.PickupDropoff_Locations_Save = function(){

    var myMask = new Ext.LoadMask('rm_pages_pickupdropoff_locations_add_mainpage', {msg:RM.Translate.Common.PleaseWait});
    myMask.show();

    var conn = new Ext.data.Connection();
    var request = {
        url: RM.Common.AssembleURL({
            controller : 'PickupDropoff',
            action: 'locationupdatejson',
            parameters : [
                {
                    name : 'id',
                    value : Ext.getCmp('rm_module_pickupdropoff_config_id').getValue()
                },{
                    name : 'name',
                    value : Ext.getCmp('rm_module_pickupdropoff_config_name').getValue()
                },{
                    name : 'address1',
                    value : Ext.getCmp('rm_module_pickupdropoff_config_address1').getValue()
                },{
                    name : 'address2',
                    value : Ext.getCmp('rm_module_pickupdropoff_config_address2').getValue()
                },{
                    name : 'address3',
                    value : Ext.getCmp('rm_module_pickupdropoff_config_address3').getValue()
                },{
                    name : 'city',
                    value : Ext.getCmp('rm_module_pickupdropoff_config_city').getValue()
                },{
                    name : 'state',
                    value : Ext.getCmp('rm_module_pickupdropoff_config_state').getValue()
                },{
                    name : 'postcode',
                    value : Ext.getCmp('rm_module_pickupdropoff_config_postcode').getValue()
                },{
                    name : 'country',
                    value : Ext.getCmp('rm_module_pickupdropoff_config_country').getValue()
                },{
                    name : 'gmaps',
                    value : Ext.getCmp('rm_module_pickupdropoff_config_gmaps').getValue()
                },{
                    name : 'enabled',
                    value : Ext.getCmp('rm_module_pickupdropoff_config_enabled').getValue()
                },{
                    name : 'units',
                    value : RM.Pages.Functions.Units_Selector_GetSelection()
                }
            ]
        }),
        method: 'POST',
        success: function(responseObject) {
            var jsonObject = RM.Common.JSON.decode(responseObject.responseText, true);
            RM.Pages.PickupDropoff_Locations_Add_MainPanel.hide();
            RM.Pages.PickupDropoff_Locations_List_Json_Store.reload();
            myMask.hide();
        },
        failure: function() {
            Ext.MessageBox.alert(RM.Translate.Common.Failed);
            myMask.hide();
        }
    };
    conn.request(request);
}
